package lumis.collaboration.chat;

import lumis.doui.control.DataBoundControl;
import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularSource;
import lumis.portal.PortalException;
import lumis.portal.controller.ControllerStringResource;
import lumis.util.IResource;
import lumis.util.XmlUtil;

import java.util.Collection;
import java.util.List;

/**
 * Renders the messages history for a chat.
 *
 * @version $Revision: 13322 $ $Date: 2011-08-08 13:48:20 -0300 (Mon, 08 Aug 2011) $
 * @since 4.0.11
 */
public class ChatHistoryControl extends DataBoundControl<TabularSource<?>>
{
	@Override
	public List<IResource> getResources() throws PortalException
	{
		if (this.resources == null)
		{
			this.resources = super.getResources();
			IResource chatResource = new ControllerStringResource();
			chatResource.addResourcePath("lumis/collaboration/chat/strings/strings");
			this.resources.add(chatResource);
		}
		return this.resources;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setRenderData() throws PortalException
	{
		super.setRenderData();

		ISourceData data = getSource().getData();
		Collection<ChatMessageConfig> messages = (Collection)data.get("messages");
		
		// add messages xml
		String xml = ChatManagerFactory.getChatManager().getMessagesXml(douiContext.getRequest().getSessionConfig(), 
				messages, false, getResources(), douiContext.getTransaction());
		XmlUtil.addNodeFromXmlString(controlDefinitionNode, xml);

		// add message list id
		String messagesListId = getId() + "_History";
		XmlUtil.addTextNode(controlDefinitionNode, "data", "messagesListId", messagesListId);
	}
}
